<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Sortable.js Free Layout</title>
    <script src="https://cdn.bootcdn.net/ajax/libs/Sortable/1.15.2/Sortable.min.js"></script>
    <style>
        .container {
            width: 200px;
            min-height: 100px;
            margin: 20px;
            padding: 10px;
            border: 2px dashed #ccc;
            float: left;
            background-color: #f9f9f9;
        }

        .item {
            padding: 10px;
            margin: 5px;
            background-color: #f0f0f0;
            border: 1px solid #ccc;
            cursor: move;
            min-width: 20px;
            display: inline;
        }

        /* 拖拽时的占位符样式 */
        .sortable-ghost {
            opacity: 0.5;
            background-color: #ffd700;
        }

        /* 被拖拽元素的样式 */
        .sortable-chosen {
            background-color: #e0e0e0;
        }

        /* 拖拽时的样式 */
        .sortable-drag {
            opacity: 0.8;
            transform: scale(1.05);
        }
    </style>
</head>
<body>
    <div id="container1" class="container">
        <div class="item">Item 1</div>
        <div class="item">Item 2</div>
        <div class="item">Item 3</div>
    </div>

    <div id="container2" class="container">
        <div class="item">Item 4</div>
        <div class="item">Item 5</div>
    </div>

    <div id="container3" class="container">
        <div class="item">Item 6</div>
    </div>

    <script>

        // 初始化排序能力
        function initSortable(){
            // 初始化所有容器
            const containers = document.querySelectorAll('.container');
            containers.forEach(container => {
                new Sortable(container, {
                    group: 'shared', // 允许跨容器拖拽
                    animation: 150,  // 拖拽动画时长
                    ghostClass: 'sortable-ghost', // 拖拽时的占位符样式
                    chosenClass: 'sortable-chosen', // 被拖拽元素的样式
                    dragClass: 'sortable-drag' // 拖拽时的样式
                    ,onAdd:function(evt){
                        console.log("evt",evt);
                    }
                });
            });
        }

        document.addEventListener("DOMContentLoaded", function() {
            initSortable();
        });
    </script>
</body>
</html>